/*
dialogframe.js可以同时在top页面，child页面中引用。
*/
var dialogframe = {
  openedDialogs : {},
  dialogIdPrix:"w",
  defaultDialogId : "dk_top_dialog_default",
  defaultShowToggle : false,
  hideDialogs : {},
  minDialogs : {},
  uniqueNum : 1,

  /**获取一个全局唯一的key*/
  getNextDialogKey : function(){
	  var nextNum = top.dialogframe.uniqueNum++;
	  return this.dialogIdPrix + nextNum;
  },
  
  	openDialog : function(options){
  		if(top.dialogframe){
  			return top.dialogframe.doOpenDialog(options);
  		}else{
  			return doOpenDialog(options);
  		}
  	},
	/**
	 *
	 * @param options
	 * {
	 * title 弹出窗口的标题
	 * url
	 * height
	 * width
	 * iframeName 新弹出窗口iframe的name
	 * isHidden
	 * isModal true|false
	 * position 子窗口的位置，可以为null { left: left, top: top }
	 * buttons
	 * }
	 */
	doOpenDialog : function(options){
		if(!options)return;
		var that = this;
		var dialogId = options.id || options.dialogId;
		
//		var dialog = this.getOpenDialog(dialogId);
//		if (dialog) {
//			dialog.close();//如果窗口已经存在，则先关闭窗口
//		}

		var thisPageUUID = that.getSelfFramePageUUID();
		if(!thisPageUUID)throw '本页面没有设置pageUUID,请定义pageUUID并设置一个全局唯一值';
		var parentPageUUID = thisPageUUID;//调用openDialog函数的页面的pageUUID
		var dialog;		
		dialog = top.dkDialog.openDialog(options);
		//dialog.pageUUID = thisPageUUID; 不要记录pageUUID，因为iframe刷新后，pageUUID就变了
		var dialogKey = this.getNextDialogKey();
		dialog.dialogId = dialogId;
		dialog.dialogKey = dialogKey;
		this.openedDialogs[dialogId]=dialog;
		
		var dialogFrameId = dialog.iframeId;
		$("#"+dialogFrameId).attr("parentPageUUID", parentPageUUID);//add by wanghua 重要，不能删除。用来实现弹出窗口和父窗口两个iframe之间的函数调用
		$("#"+dialogFrameId).attr("dialogKey", dialogKey);
		
		dialog.bind('onClosed',function(){
			delete that.openedDialogs[dialogId];
			if(that.minDialogs[dialogId] && top.mapFrame){
				var minDialogList = top.mapFrame.document.getElementById("minDialogList");
				$("#"+dialogId,$(minDialogList)).remove();
				delete that.minDialogs[dialogId];
			}
		});

		//最小化
		dialog.bind('onMinized',function (){
			that.minDialog(dialogId);
	  	});


		if(dialogId=='helpLocation'){
			$("#helpLocation").css("z-index", "9300").attr("resetzindex",false);
		}

		return dialog;
	},
	
	getSelfFramePageUUID : function(){
		//var selfFrame = this.getSelfFrame();
		//var pageUUID = selfFrame.pageUUID || selfFrame.contentWindow.pageUUID;//selfFrame如果是top，则selfFrame.pageUUID
		return pageUUID;
	},
	
	/**返回的可能是top，或者iframe*/
	getSelfFrame : function (rootDoc){
		if(window == top)return window;//当前页面是顶级页面
		var thisWindow = window;		
		var iframes=$("iframe", rootDoc || parent.window.document);
		var selfFrame = window.frameElement || null; //window.frameElement 包含本页面的iframe或frame对象。 
		for(var i=iframes.length-1;i>-1&&!selfFrame;i--){
			var iframe=iframes[i];
			if(iframe.src.indexOf(window.location.hostname)==-1){//监测是否跨域
				;
			}else if(thisWindow.pageUUID && thisWindow.pageUUID == iframe.contentWindow.pageUUID){
				selfFrame=iframe;
			}else if(iframe.id&&iframe.contentDocument){ //iframe 下面可以再嵌入一层iframe
				selfFrame=this.getSelfFrame(iframe.contentDocument);
			}
		}
		if(selfFrame==null)
			selfFrame="";
		return selfFrame;
	},
	
	/**返回值 可能为top window或者iframe*/
	getFrameByPageUUIDId : function(pageUUID, rootDoc){//根据pageUUID检索iframe
		if(!pageUUID)return null;
		if( pageUUID == top.pageUUID)return top;
		
		var iframes=$("iframe", rootDoc || window.top.document);
		var findedFrame=null;
		for(var i = iframes.length-1; i > -1 && !findedFrame; i--){
			var iframe=iframes[i];
			var currFramePageUUID = iframe.contentWindow.pageUUID;
			if(iframe.src.indexOf(window.location.hostname)==-1){//检测是否跨域
				;
			}else if(currFramePageUUID == pageUUID){
				findedFrame=iframe;
			}else if(iframe.contentDocument){ //iframe 下面可以再嵌入一层iframe
				findedFrame = this.getFrameByPageUUIDId(pageUUID, iframe.contentDocument);
			}
		}
		return findedFrame;
	},

	closeDialog : function(event){
		/**在调用openDialog的页面找到对于的dialog，并关闭它
		 * 
		 */
		var selfPageUUID = this.getSelfFramePageUUID();
		var parentFrame = this.getParentFrame();//parentFrame 可能为top window或者iframe
		
		var parentDialogframe = parentFrame.dialogframe || parentFrame.contentWindow.dialogframe;
		var dlg = parentDialogframe.findDialog(selfPageUUID);
		if(dlg){
			dlg.close(event);
		}
	},
	
	/**返回当前页面记录的pageUUID为传入参数值的弹出窗口*/
	findDialog : function(pageUUID){
		//一定要注意：父页面不能记录子页面的pageUUID，因为子页面刷新的时候，pageUUID就变了
		var iframe = this.getFrameByPageUUIDId(pageUUID);//根据pageUUID找到所在的iframe
		var dialogKey = $(iframe).attr("dialogKey");//根据iframe的属性获取dialogKey
		for(var dialogId in this.openedDialogs){//根据dialogKey获取dialog
			var dlg = this.openedDialogs[dialogId];
			if(dlg.dialogKey == dialogKey){
				return dlg;
			}
		}
	},
	
	getParentFrame : function(){
		var parentPageUUID = $(this.getSelfFrame()).attr("parentPageUUID");
		if(!parentPageUUID)throw '在iframe的属性中没有找到parentPageUUID,url:'+location.href;
		var parentFrame = this.getFrameByPageUUIDId(parentPageUUID);
		return parentFrame;
	},
	/**
	 * 执行父窗口页面内的函数
	 * @param funcName 函数名
	 * @param args 函数参数，数组类型
	 */
	invokeParentFunction : function(funcName, args){
		var parentFrame = this.getParentFrame();
		if(parentFrame){//parentFrame 可能为top window或者iframe
			var childDialogframe = parentFrame.dialogframe || parentFrame.contentWindow.dialogframe;
			childDialogframe.invoke(funcName,args);
		}else{
			throw '没有找到  pageUUID='+parentPageUUID+' 的页面';
		}
	},
	
	invoke : function(funcName, args){
		var func = eval(funcName);
		if(!args) args = [];
		func.apply(this,args);
	},
	

	/**
	 *@param dialogId dialogId或者dialogKey
	 */
	getOpenDialog : function(dialogId){
		//return dkDialog.get(dialogId)||top.window.dkDialog.get(dialogId);
		for(var dialogId in this.openedDialogs){
			var dlg = this.openedDialogs[dialogId];
			if(dlg.dialogId == dialogId || dlg.dialogKey == dialogId){
				return dlg;
			}
		}
	},
	//刷新对话框内容
	refreshDialogContent : function(dialogId,url){
		dialogId=dialogId || this.defaultDialogId;
		var dialog=dkDialog.get(dialogId);
		if(!dialog){
			dialog=top.window.dkDialog.get(dialogId);
		}
		if(dialog&&dialog.get("visible")){
			dialog.setUrl(url);
		}
	},
	closeDialogById : function(dialogId){
		dialogId=dialogId || this.defaultDialogId;
		var dialog=dkDialog.get(dialogId);
		if(!dialog){
			dialog=top.window.dkDialog.get(dialogId);
		}
		if(typeof dialogId=="object"){
			dialog=dialogId;
		}
		if(dialog){
			var isHidden=dialog.get("isHidden");
			if(isHidden){
				dialog.hide();
				if(this.minDialogs[dialogId]&&top.mapFrame){
					var minDialogList=top.mapFrame.document.getElementById("minDialogList");
					$("#"+dialogId,$(minDialogList)).remove();
					delete this.minDialogs[dialogId];
				}
			}else{
				dialog.close();
			}
		}
	},

	hideDialog : function (dialogId){
		dialogId=dialogId || this.defaultDialogId;
		var dialog=dkDialog.get(dialogId);
		if(!dialog){
			dialog=top.window.dkDialog.get(dialogId);
		}
		if(typeof(dialogId)=="object"){
			dialog=dialogId;
		}
		if(dialog&&dialog.get("visible")){
			dialog.hide();
		}
	},


	//隐藏所有打开的对话框
	hideAllDialog : function (){
		for(var dialogId in this.openedDialogs){
			var dialog = this.openedDialogs[dialogId];
			if(dialog.get("visible")){
				dialog.hide();
				this.hideDialogs[dialogId]=dialog;
			}
		}
	},

	//打开使用hideAllDialog隐藏的对话框
	showAllDialog : function (){
		for(var dialogId in this.hideDialogs){
			this.showDialog(dialogId);
		}
		this.hideDialogs={};
	},

	showDialog : function (dialogId){
		dialogId=dialogId || this.defaultDialogId;
		var dialog=dkDialog.get(dialogId);
		if(!dialog){
			dialog=top.window.dkDialog.get(dialogId);
		}
		if(dialog){
			dialog.show();
			dialog.active();
			if(this.minDialogs[dialogId]&&top.mapFrame){
				var minDialogList=top.mapFrame.document.getElementById("minDialogList");
				$("#"+dialogId,$(minDialogList)).remove();
				delete this.minDialogs[dialogId];
			}
		}
	},
	//最小化对话框窗口到主窗口下的minDialogList层内；
	minDialog : function(dialogId){
		if(!top.mapFrame)return;
		var minDialogList=top.mapFrame.document.getElementById("minDialogList");
		if(!minDialogList){
			return;
		}
		dialogId=dialogId || this.defaultDialogId;
		var dialog=dkDialog.get(dialogId);
		if(!dialog){
			dialog=top.window.dkDialog.get(dialogId);
		}
		if(dialog){
			dialog.hide();
			var dialogTitle=dialog.get("title");
			if(dialogTitle.length>6){
				dialogTitle = dialogTitle.substr(0,5)+"...";
			}
			var minDialogdiv='<div class="minDialog" id="'+dialogId+'">\
				<div style="width:85px;float:left">\
				<span class="minDialogTitle" onclick=javascript:top.window.showDialog("'+dialogId+'") title="'+dialog.get("title")+'">'+dialogTitle+'</span></div>\
				<div style="width:26px;float:right;margin-top:4px;">\
				<span onclick=javascript:top.window.closeDialog("'+dialogId+'") title="关闭对话框" class="closeBtn">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span></div></div>';
			$(minDialogList).append(minDialogdiv);
			this.minDialogs[dialogId]=dialogId;
		}
	},

	//最小化所有对话框
	minAllDialog : function (){
		for(var dialogId in this.openedDialogs){
			var dialog = this.openedDialogs[dialogId];
			if(dialog.get("visible")){
				this.minDialog(dialogId);
			}
		}
	},

	//最大化所有最小化的对话框
	maxAllDialog : function (){
		for(var dialogId in this.minDialogs){
			this.showDialog(dialogId);
		}
		this.minDialogs={};
	},

	setDialogTitle : function(dialogId,title){
		var dialog = this.openedDialogs.get(dialogId);
		if(dialog&&dialog.get("visible")){
			dialog.set("title",title);
		}
	},

	//遮罩id=dialogId以外的所有元素
	maskWindow : function(dialogId){
		var dialog=dkDialog.get(dialogId);
		if(dialog&&dialog.get("visible")){
			dialog.active();
			//$(".l-window-mask").css("z-index",($("#"+dialogId).css("z-index")-1));
			dialog.mask();
		}
	},


	/**
	 * 读取iframe页面内的变量,或函数
	 * @param varName 变量名或函数名
	 * @param iframeName 变量所在页面的名字
	 */
	getValueOfChildFrame : function(iframeName, varName){
		iframeName=iframeName||"cacheDataIframe";
		var iframeDoc=top.window.frames[iframeName];
		var varName="iframeDoc."+varName;
		if(iframeDoc){
			return eval(varName);
		}
		return undefined;
	},
	/**
	 * 修改iframe页面内的变量
	 * @param varName 变量名或函数名
	 * @param iframeName 变量所在页面的名字
	 */
	setValueOfChildFrame : function(iframeName, varName,value){
		iframeName=iframeName||"cacheDataIframe";
		var iframeDoc=top.window.frames[iframeName];
		if(iframeDoc){
			iframeDoc[varName]=value;
			return true;
		}
		return false;
	},
	/**
	 * 调用指定dialog中的方法
	 * @param {} dialogId
	 * @param {} funcName
	 * @param {} args
	 * @return {}
	 */
	invokeFunctionOfChild : function(dialogId, funcName, args){

		var iframeDoc=top.window.frames[dialogId];

		var funcName2="iframeDoc."+funcName;
		if(iframeDoc){
			var func = eval(funcName2);
			//alert(args);
			return func.apply();
		}
		return undefined;
	},

	/**
	 * 读取iframe页面内的变量,或函数
	 * @param varName 变量名或函数名
	 * @param iframeName 变量所在页面的名字
	 */
	getValueOfMainFrame : function(varName){
		return top.window[varName];
	}
};

if(!top.dialogframe){
	top.dialogframe = dialogframe;
}
